package a66weding.com.jiehuntong.common.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import a66weding.com.jiehuntong.application.DataWareHourse;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/6/30
 * @description :
 */
public class FileUtil {


    public static String   HEAD_IAMGE = "head_image.jpg";

    public static File getImgFile(Context context){
        File saveDir = DataWareHourse.getInstance().getSaveDir(context);
        File imgFileDir=new File(saveDir,"Images");
        if(!imgFileDir.exists()) {
            imgFileDir.mkdirs();
        }
        return imgFileDir;
    }



    public static Bitmap getBitmapFromDisk(String name,Context context){
        Bitmap bitmap = null;
        try {
            //获得指定的文件;
            File file = new File(FileUtil.getImgFile(context),name);
            if(!file.exists()){
                return null;
            }
            bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public static File getDbFile(Context context){
        File saveDir = DataWareHourse.getInstance().getSaveDir(context);
        File dbFileDir=new File(saveDir,"database");
        if(!dbFileDir.exists()) {
            dbFileDir.mkdirs();
        }
        return dbFileDir;
    }


    public static File getDownloadFile(Context context){
        File saveDir = DataWareHourse.getInstance().getSaveDir(context);
        File dbFileDir=new File(saveDir,"download");
        if(!dbFileDir.exists()) {
            dbFileDir.mkdirs();
        }
        return dbFileDir;
    }

    /**
     * 根目录/66Wedding/
     * @param fileName
     * @return
     */
    public static File getDefaultFile(String fileName){
        String path = Environment.getExternalStorageDirectory().getPath();
        File fileDir = new File(path + "/66Wedding");
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }
        File file = new File(fileDir.getPath() + "/" + fileName);
        return file;
    }

    /**
     * 默认目录
     * @return
     */
    public static File getDefaultDir(){
        String path = Environment.getExternalStorageDirectory().getPath();
        File fileDir = new File(path + "/66Wedding");
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }
        return fileDir;
    }
}
